package com.sptus.controller;

import com.sptus.aop.AppIntercept;
import com.sptus.common.constant.SymbolConstant;
import com.sptus.common.utils.CommonUtils;
import com.sptus.common.utils.uuid.UUID;
import com.sptus.service.S3UploadService;
import com.sptus.util.oConvertUtils;
import com.sptus.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import org.springframework.util.FileCopyUtils;

import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.HashMap;

@Slf4j
@RestController
@Api(tags = "app文件上传")
@RequestMapping("/sys/app/common")
public class UploadFile {
    @Value(value = "${spt.path.upload}")
    private String uploadpath;

    /**
     * 本地：local minio：minio 阿里：alioss
     */
    @Value(value="${spt.uploadType}")
    private String uploadType;
    @Value(value = "${spt.domainUrl.app}")
    private  String appUrl;


    @Autowired
    private S3UploadService uploadService;

    @ApiOperation("文件上传")
    @AppIntercept
    @PostMapping(value = "/upload")
    public Result<Object> uploadLocal(MultipartFile file){



//        String fileName = UUID.fastUUID()+file.getOriginalFilename();
//        String url = uploadService.uploadFile(file,fileName);


        String bizPath="upload";
        Result<Object> result = new Result<>();
        result.setCode(200);
        try {
            String ctxPath = uploadpath;
            String fileName = null;
//            File fileLe= new File(ctxPath + File.separator + bizPath + File.separator );
//            if (!fileLe.exists()) {
//                // 创建文件根目录
//                fileLe.mkdirs();
//            }
            // 获取文件名
            String orgName = file.getOriginalFilename();
            orgName = CommonUtils.getFileName(orgName);
            if(orgName.indexOf(SymbolConstant.SPOT)!=-1){
                fileName = orgName.substring(0, orgName.lastIndexOf(".")) + "_" + System.currentTimeMillis() + orgName.substring(orgName.lastIndexOf("."));
            }else{
                fileName = orgName+ "_" + System.currentTimeMillis();
            }
//            String savePath = fileLe.getPath() + File.separator + fileName;
//            File savefile = new File(savePath);
//            FileCopyUtils.copy(file.getBytes(), savefile);
//            String dbpath = null;
//            if(oConvertUtils.isNotEmpty(bizPath)){
//                dbpath = bizPath + File.separator + fileName;
//            }else{
//                dbpath = fileName;
//            }
//            if (dbpath.contains(SymbolConstant.DOUBLE_BACKSLASH)) {
//                dbpath = dbpath.replace(SymbolConstant.DOUBLE_BACKSLASH, SymbolConstant.SINGLE_SLASH);
//            }

            String url = uploadService.uploadFile(file,fileName);
            HashMap<Object, Object> objectObjectHashMap = new HashMap<>();
            objectObjectHashMap.put("url",url);//appUrl+dbpath);
            result.setResult(objectObjectHashMap);
            return result;
        } catch (IOException e) {
            log.error(e.getMessage(), e);
        }
        return result;
    }
}
